package com.jstyle.beatecg.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import com.jstyle.beatecg.util.ScreenUtils;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/12/22.
 */

public class RadioView extends View {
    private List<String> titles;
    private Paint textPaint;
    private int defaultWidth;
    private int defaultHeight;
    float count = 5;
    Paint mPaint;
    Paint mRegionPaint;
    float radius;
    float circleRadius = 13;
    int lineColor = Color.BLACK;
    int circleColor = Color.RED;
    int textColor = Color.WHITE;
    int regionColor = Color.WHITE;
    private float angel;
    float[]points=new float[5];
    float[]heartAge=new float[]{95,80,65,40,25};
    public RadioView(Context context) {
        super(context);
    }

    public RadioView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        defaultHeight = ScreenUtils.dip2px(context, 220);
        defaultWidth = ScreenUtils.dip2px(context, 200);
        radius = (float) ScreenUtils.dip2px(context, 90);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(1);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(lineColor);

        mRegionPaint = new Paint();
        mRegionPaint.setAntiAlias(true);
        mRegionPaint.setStrokeWidth(10);
        mRegionPaint.setStyle(Paint.Style.STROKE);
        mRegionPaint.setColor(regionColor);


        textPaint = new Paint();
        textPaint.setColor(textColor);
        textPaint.setTextAlign(Paint.Align.CENTER);
        float dimension = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getContext().getResources().getDisplayMetrics());

        textPaint.setTextSize(dimension);
        textPaint.setStrokeWidth(1);
        textPaint.setAntiAlias(true);

        titles = new ArrayList<String>();

    }

    float centerX;
    float centerY;
    public void setTitles(List<String>titles){
        this.titles=titles;
        postInvalidate();
    }
    public  void updateData(float[]hrv){
        this.points=hrv;
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        centerX = getWidth() / 2;
        centerY = getHeight() / 2;
        drawPolygon(canvas);
        drawLines(canvas);
        drawRegion(canvas);
        drawTitle(canvas);

    }

    int max = 100;

    private void drawRegion(Canvas canvas) {
        Path path = new Path();
        for (int i = 0; i < count; i++) {
            float percent=0;
            if(i==2){
                if(points[i]==0)continue;
                percent=heartAge[(int) points[i]-1]/max;
            }else{
                percent= points[i] / max;
            }
            float score = percent>=1?radius:percent * radius;

            float x = (float) (centerX + score * Math.sin(angel * i));
            float y = (float) (centerY - score * Math.cos(angel * i));
            if (i == 0) {
                path.moveTo(x, y);
            }
            path.lineTo(x, y);
           // mRegionPaint.setAlpha(255);
            mRegionPaint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(x, y, circleRadius, mRegionPaint);
        }
        path.close();
        mRegionPaint.setStyle(Paint.Style.STROKE);
        canvas.drawPath(path, mRegionPaint);
    }

    private void drawPolygon(Canvas canvas) {
        angel = (float) (2 * Math.PI / count);
        float senRadius = radius / 3;
        Path path = new Path();
        for (int i = 3; i >=0; i--) {
            float mRadius = senRadius * i;
            path.reset();
            for (int j = 0; j < count; j++) {
                if (j == 0) {
                    path.moveTo(centerX, centerY - mRadius);
                } else {
                    float x = (float) (centerX + mRadius * Math.sin(angel * j));
                    float y = (float) (centerY - mRadius * Math.cos(angel * j));
                    path.lineTo(x, y);

                }

            }
            path.close();
            if(i==3){
                mPaint.setColor(Color.parseColor("#fa5528"));
            }else if(i==2){
                mPaint.setColor(Color.parseColor("#2ef84a"));
            }else{
                mPaint.setColor(Color.parseColor("#46c7ee"));
            }
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawPath(path, mPaint);
        }

    }

    private static final String TAG = "RadioView";

    private void drawLines(Canvas canvas) {
        Path path = new Path();
        mPaint.setColor(lineColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAlpha(255);
        for (int i = 0; i < count; i++) {
            path.reset();
            path.moveTo(centerX, centerY);
            float x = (float) (centerX + radius * Math.sin(angel * i));
            float y = (float) (centerY - radius * Math.cos(angel * i));
            path.lineTo(x, y);

            canvas.drawPath(path, mPaint);
        }

    }

    private void drawTitle(Canvas canvas) {
//        if (count != titles.size()) {
//            return;
//        }
        if(titles.size()==0)return;
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float fontHeight = fontMetrics.descent - fontMetrics.ascent;
        //绘制文字时不让文字和雷达图形交叉,加大绘制半径
        float textRadius = radius + fontHeight;
        double pi = Math.PI;
        for (int i = 0; i < count; i++) {
            float x = (float) (centerX + textRadius * Math.sin(angel * i));
            float y = (float) (centerY - textRadius * Math.cos(angel * i));
            //当前绘制标题所在顶点角度
            float degrees = angel * i;
            float dis = textPaint.measureText(titles.get(i)) / (titles.get(i).length() - 1);
            //从右下角开始顺时针画起,与真实坐标系相反

            if (degrees > 0 && degrees < pi) {
                x = x + dis;
            } else if (degrees > pi && degrees < pi * 2) {
                x = x - dis;
            }
            canvas.drawText(titles.get(i), x, y, textPaint);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
            height = defaultHeight;
        }
        if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) {
            width = defaultWidth;
        }
        setMeasuredDimension(width, height);
    }
}
